package com.hjqjl.whdemo.dialog.dialogfragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.hjqjl.whdemo.R;

/**
 * 顶部显示、全屏、宽度全屏、高度自定义、背景全透明；
 * 仿网易云音乐下载完成提示；
 * 设置属性和点击事件可参考自定义布局CustomDialogFragment
 */
public class DialogTopFragment extends DialogFragment {

    public static DialogTopFragment newInstance() {
        DialogTopFragment fragment = new DialogTopFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View dialogView = inflater.inflate(R.layout.dialog_top, container);
        return dialogView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFullScreen); //dialog全屏
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = getDialog().getWindow();
            if (window != null) {
                WindowManager.LayoutParams windowParams = window.getAttributes();
                windowParams.dimAmount = 0.0f;//Dialog外边框透明
                window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT); //高度自适应，宽度全屏
                windowParams.gravity = Gravity.TOP; //在顶部显示
                windowParams.windowAnimations = R.style.TopDialogAnimation;
                window.setAttributes(windowParams);
            }
        }
    }
}
